<?php
require_once "common.php";
define('INIT', time());

define('START', 'id="J_AllCates"');
define('END', '</dl>');
define('URL', "http://s8.taobao.com/search?cat=");
$id = '50002766';

$cats = get_cats($id);
$cats_all = array();
foreach($cats as $v) {
    $cats_all = array_merge($cats_all, get_cats($v['cat_id']));
}
$cats_all = array_merge($cats_all, $cats);


function get_cats($id) {
    $cats = array();
    $txt = _curl_post(URL.$id);
    $str = explode(START, $txt);
    if(count($str)==2) {
        $str = stristr($str[1], END, 1);
        $n = preg_match_all("/<a href=\"[^\"]*-c-(\d+)\".*?>([^<]*)<\/a>/is", $str, $cat);
        if($n) {
            $ids = $cat[1];
            $names = $cat[2];
            $sort = 0;
            foreach($ids as $k=>$v) {
                $cats[] = array('cat_id'=>$v, 'name'=>$names[$k], 'parent_id'=>$id, 'sort'=>++$sort);
            }
        }
    }
    echo "\r".(time()-INIT);
    return $cats;
}
?>
